package com.vunguyen.service;


import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vunguyen.dao.UserDAO;
import com.vunguyen.model.Canbo;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDAO userDAO;

	@Override
	@Transactional
	public boolean checkLogin(int username, String password) {
		return this.userDAO.checkLogin(username, password);
	}

	@Override
	@Transactional
	public String getPhanQuyen(int username) {
	
		if(getQuyenDangNhap(username) == 1) {
			return "redirect:/thuky";
		} 
		else if(getQuyenDangNhap(username) == 2){
			return "redirect:/giangvien";
		}
		else if(getQuyenDangNhap(username) == 3){
			return "redirect:/banchunhiem";
		}
		else if(getQuyenDangNhap(username) == 4){
			return "redirect:/thanhtra";
		}
		else{
			return "redirect:/bomon";
		}
		
			
	}

	@Override
	@Transactional
	public int getQuyenDangNhap(int username) {
		try{
		Canbo cb = userDAO.getTaikhoanbyUsername(username);
		return cb.getQuyen();
		}
		catch(Exception e){
			return 0;
		}
	}

	@Override
	@Transactional
	public Canbo getTenCanBoById(int idcanbo) {
		return userDAO.getTenCanBoById(idcanbo);
	}
	//1 la thu ky, 2 la giang vien, 3 la ban chu nhiem, 4 la thanh tra, 5 la bo mon
	@Override
	@Transactional
	public String getChucVuByQuyen(int username) {
		if(getQuyenDangNhap(username) == 1) {
			return "TK";
		} 
		else if(getQuyenDangNhap(username) == 2){
			return "GV";
		}
		else if(getQuyenDangNhap(username) == 3){
			return "BCN";
		}
		else if(getQuyenDangNhap(username) == 4){
			return "Thanh tra";
		}
		else{
			return "BM";
		}
	}

}
